package org.wattdepot.cli.command;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.cli.processor.CommandLineInterface;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.util.tstamp.Tstamp;

/**
 * Holds a method to do the power command which checks power for day and interval checks for
 * WattDepot.
 * 
 * @author Kendyll Doi
 */
public class WattPowerStats {
  /**
   * Reports the Max, Min, or Average Power Generated or Consumed from a source over a time for 
   * a day.
   * 
   * @param type The type of data (generated or consumed) to gather
   * @param source The name of the source to gather information on
   * @param date The day to gather information in
   * @param minutes The sampling interval to use to gather data
   * @param statistic The type of statistic to return (max, min, or average)
   * @param client The client for the CLI
   * @return a sting containing the results of the method.
   */
  public static String wattListPowerDay(String type, String source, String date, String minutes, 
      String statistic, WattDepotClient client) {
    String out = null;
    int minuteInterval = 0;
    int day = 0;
    List<Double> powervalues = new ArrayList<Double>();
    double totalPower = 0;
    try {

      XMLGregorianCalendar time = CommandLineInterface.createTstamp(date);
      if (time == null) {
        return "Time argument not correct";
      }
      day = time.getDay();
      minuteInterval = Integer.parseInt(minutes);
      if (minuteInterval == 0) {
        out = "Cannot use interval 0, no time will measure";
        return out;
      }
      if (type.equalsIgnoreCase("generated")) {

        while (time.getDay() == day) {
          // System.out.println("time: " + time.getDay() + " day: " + day);
          // System.out.println(client.getPowerGenerated(source, time) + " at:" + time);
          powervalues.add(client.getPowerGenerated(source, time));
          totalPower += client.getPowerGenerated(source, time);
          time = Tstamp.incrementMinutes(time, minuteInterval);
        }
      }

      else if (type.equalsIgnoreCase("consumed")) {
        while (time.getDay() == day) {
          powervalues.add(client.getPowerConsumed(source, time));
          totalPower += client.getPowerConsumed(source, time);
          time = Tstamp.incrementMinutes(time, minuteInterval);
        }
      }
      else {
        out = "Command error with type of power requested";
        return out;
      }

      if (statistic.equalsIgnoreCase("average")) {
        out = Double.toString(totalPower / powervalues.size());
      }
      else if (statistic.equalsIgnoreCase("max")) {
        out = Collections.max(powervalues).toString();
      }
      else if (statistic.equalsIgnoreCase("min")) {
        out = Collections.min(powervalues).toString();
      }
      else {
        out = "Statistic type not recognized.";
      }
      return out;
    }
    catch (ResourceNotFoundException e) {
      System.out.println("No data found for \"" + source + "\". It might not exist.");
      e.printStackTrace();
    }
    catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return out;

  }
}
